//
//  AppDelegate.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/2/27.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "AppDelegate.h"
#import "HomeViewController.h"
#import "LoginViewController.h"
#import "BigPicViewController.h"
// 引入JPush功能所需头文件
#import "JPUSHService.h"
// iOS10注册APNs所需头文件
#define JPushAPPKey  @"511b435d078ac6ce0b6a7093"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif


@interface AppDelegate ()<JPUSHRegisterDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
   // [Userdefault setUser:@{@"accessToken":@"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJ3d3cuZXRvbXdlYi5jb20iLCJsb2dpbklkIjoiY3lsIiwiaXNzIjoiZWJpaG0iLCJpZCI6ImNmZjZiNGVjZTZiNDQwNGJhMDU5ODA3YmFmZTVhZDkzIiwiaWF0IjoxNDkxNDYyNDY4Mjc2LCJ1c2VySWQiOiI4NjU4MTI2MjhkZjE0ZjdiOTcyYzE2ODg2ZTA0Yjk3ZSJ9.Tlg3tE37opQDn6dMaoB9Kh9SvoPBCOAxEchgc7jPbGaIieGhd5LhkgwtWAXn_HwipX7Lt5UC0c8WyJs_TquvAQ",@"userName":@"王大路"}];
    
     [self allSetUp];
    JPUSHRegisterEntity * entity = [[JPUSHRegisterEntity alloc] init];
    entity.types = JPAuthorizationOptionAlert|JPAuthorizationOptionBadge|JPAuthorizationOptionSound;
    [JPUSHService registerForRemoteNotificationConfig:entity delegate:self];
    [JPUSHService setupWithOption:launchOptions appKey:JPushAPPKey
                          channel:@"App Store"
                 apsForProduction:0];
    
    LoginViewController *con = [[LoginViewController alloc]init];
    navigationC *nav = [[navigationC alloc]initWithRootViewController:con];
    self.window.rootViewController = nav;
    return YES;
    
}
#pragma -mark  基础全局设置
- (void)allSetUp{
    //弹键盘设置
    IQKeyboardManager *manager = [IQKeyboardManager sharedManager]; 
    manager.enable = YES;
    manager.shouldResignOnTouchOutside = YES;
    manager.shouldToolbarUsesTextFieldTintColor = YES;
    manager.enableAutoToolbar = NO;
    [[UITextField appearance] setTintColor:Gray_Color];
    [[UITextView appearance] setTintColor:Gray_Color];
    
}
//手机Pad横竖屏支持情况
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window{
        if (IsPad) {
        return UIInterfaceOrientationMaskAll;
        }else{
        return UIInterfaceOrientationMaskPortrait;
        }
        
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    /// Required - 注册 DeviceToken
    
    //下面这个token是将获取的nsdata转换成String,应为指定推送时我们需要将这个传给服务端。
    NSString *token1=[NSString stringWithFormat:@"%@",[[[[deviceToken description] stringByReplacingOccurrencesOfString: @"<" withString: @""]                  stringByReplacingOccurrencesOfString: @">" withString: @""]                 stringByReplacingOccurrencesOfString: @" " withString: @""]];
  
    NSLog(@"token%@",token1);
    [JPUSHService registerDeviceToken:deviceToken];
    EXDeviceToken = token1;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    [[NSNotificationCenter defaultCenter]postNotificationName:@"NewVersion" object:nil];
    [[NSNotificationCenter defaultCenter]postNotificationName:@"homeReload" object:nil];

}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

#pragma mark- JPUSHRegisterDelegate

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(NSInteger))completionHandler {
    // Required
    NSDictionary * userInfo = notification.request.content.userInfo;
    if([notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler(UNNotificationPresentationOptionAlert); // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以选择设置
}

// iOS 10 Support
- (void)jpushNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)())completionHandler {
    // Required
    NSDictionary * userInfo = response.notification.request.content.userInfo;
    if([response.notification.request.trigger isKindOfClass:[UNPushNotificationTrigger class]]) {
        [JPUSHService handleRemoteNotification:userInfo];
    }
    completionHandler();  // 系统要求执行这个方法
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    
    // Required, iOS 7 Support
    [JPUSHService handleRemoteNotification:userInfo];
    completionHandler(UIBackgroundFetchResultNewData);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
    
    // Required,For systems with less than or equal to iOS6
    [JPUSHService handleRemoteNotification:userInfo];
}
@end
